var InterValObj; //timer变量，控制时间
var count = 60; //间隔函数，1秒执行
var curCount;//当前剩余秒数
// var host = '127.0.0.1:8000';

function sendMessage() {
    curCount = count;
    var mobile = $('.mobile').val();

    if (!mobile) {
        $(".mobile_error").show()
    }
    //设置button效果，开始计时
    $(".sms-button").attr("disabled", "true");
    $(".sms-button").val("请在" + curCount + "秒内输入验证码");
    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
    //向后台发送处理数据
    $.ajax({
        url: "/sms_codes/" + mobile, //目标地址
        type: "get", //用POST方式传输
        dataType: "json", //数据格式:JSON
        data: {
            "mobile": mobile,
            "csrfmiddlewaretoken": $("*[name='csrfmiddlewaretoken']").val(),
        },
        headers: {
            'X-CSRFToken': $.cookie('csrftoken')
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        },
        success: function (msg) {
            // 走通发送短信验证码流程
            SetRemainTime()
        }
    });
}

//timer处理函数
function SetRemainTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
        $(".sms-button").removeAttr("disabled");//启用按钮
        $(".sms-button").attr("onclick", "sendMessage()");
        $(".sms-button").html("重新发送验证码");
    } else {
        console.log(curCount);
        curCount--;
        // $(".sms-button").val('');
        $(".sms-button").html("请在" + curCount + "秒内输入验证码");
        $(".sms-button").attr("onclick", "")
    }
}

function CheckRegister(mobile, smscode, password) {

    if (!mobile) {
        $('.mobile_error').css('display', 'block');
        return;
    }
    if (!smscode) {
        $('.sms_code_error').css('display', 'block');
        return;
    }
    if (!password) {
        $('.pwd_error').css('display', 'block');
    }
}

$(function () {

    // TODO 注册
    $('.ui-button').click(function () {
        event.preventDefault();
        // 获取注册信息
        mobile = $('.mobile').val();
        smscode = $('.SmsCode').val();
        password = $('.password').val();

        // 校验注册信息
        CheckRegister(mobile, smscode, password);

        //提交信息
        var params = {
            "mobile": mobile,
            "smscode": smscode,
            "password": password,
            "csrfmiddlewaretoken": $("*[name='csrfmiddlewaretoken']").val(),
        };
        $.ajax({
            url: "/register/",
            type: "post",
            data: JSON.stringify(params),
            headers: {
                'X-CSRFToken': $.cookie('csrftoken')
            },
            async: false,
            contentType: "application/json",
            success: function (resp) {
                if (resp.errno == "0") {
                    alert('注册成功');
                    window.location = "/";
                } else {
                    if (resp.errno == '1112') {
                        data = resp.errmsg;
                        $('.mobile_error').html(data);
                        $('.mobile_error').show()
                    } else if (resp.errno == '1114') {
                        data = resp.errmsg;
                        $('.pwd_error').html(data);
                        $('.pwd_error').show()
                    } else if (resp.errno == '1118') {
                        data = resp.errmsg;
                        $('.mobile_error').html(data);
                        $('.mobile_error').show()
                    }

                }
            },
        });
    });
});